/* eslint-disable */
export namespace StringUtil {
  export enum Operator {
    /** 和 */
    PLUS = 0,
    /** 差 */
    REDUCE,
    /** 乘 */
    MULT,
    /** 除 */
    DIVIDE,
  }

  /** 字符串转运算符 */
  export function stringToOperator(str: string) {
    // if (str.indexOf('/') != -1) {
    const ret = [];
    if (str === '' || str === undefined) return ret;
    // const strArr = StringUtil.strSplitMore(str, String, '/');
    // strArr.forEach((tempStr) => {
    // const tempRet = [];
    let operator = null;
    let number = null;
    if (str.indexOf('%') != -1) {
      operator = str.replace(/\d+(\.\d+)?%/g, '');
      number = str.match(/\d+(\.\d+)?%/g);
    } else {
      operator = str.replace(/\d+(\.\d+)?/g, '');
      number = str.match(/\d+(\.\d+)?/g);
    }
    // const operator = tempStr.replace(/-?\b\d+(\.\d+)?\b/g, '');
    // const number = tempStr.match(/-?\b\d+(\.\d+)?\b/g);
    /** 匹配整数或小数 (\d+[.?]\d+|\d+) */
    /** 匹配整数百分数或小数百分数 (\d+[.?]\d+[%?]|\d+[%?]) */
    if (operator.length <= 0 || number.length <= 0) {
      ret.push(str);
    } else {
      switch (operator) {
        case '+':
          ret.push(Operator.PLUS);
          break;
        case '-':
          ret.push(Operator.REDUCE);
          break;
        case '*':
          ret.push(Operator.MULT);
          break;
        case '/':
          ret.push(Operator.DIVIDE);
          break;
        case '&':
          break;
        case '|':
          break;
        case '%':
          break;
        case '^':
          break;
      }
      if (str.indexOf('%') !== -1) {
        const n = number.shift();
        const n1 = n.match(/\d+/g);
        switch (ret[0]) {
          case Operator.PLUS:
            ret[0] = Operator.MULT;
            ret.push(1 + Number(n1.shift()) / 100);
            break;
          case Operator.REDUCE:
            ret[0] = Operator.MULT;
            ret.push(1 - Number(n1.shift()) / 100);
            break;
        }
      } else {
        ret.push(Number(number.shift()));
      }
    }
    // ret.push(tempRet);
    // });
    return ret;
  }

  export function numberToString(num: number): string {
    return num.toLocaleString();
  }

  export function stringToNumber(str: string): number {
    const reg1 = new RegExp(',', 'g'); // 加'g'，删除字符串里所有的"a"
    return Number.parseInt(str.replace(reg1, ''));
  }

  /**
   * 获取数字的位数
   */
  export function getDigit(integer) {
    let digit = -1;
    while (integer >= 1) {
      digit++;
      integer /= 10;
    }
    return digit;
  }

  /**
   * 为数字加上单位：万、亿、兆等
   */
  export function addWan(integer, number, multiple, decimalDigit) {
    const units = ['万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载'];
    const unitIndex = Math.floor((multiple / 4) % units.length);
    const unit = units[unitIndex];
    const divisor = Math.pow(10, multiple);
    const roundedNumber = Math.round((number / divisor) * Math.pow(10, decimalDigit)) / Math.pow(10, decimalDigit);
    return `${roundedNumber}${unit}`;
  }

  /**
   * 将数字转换为带有中文单位的字符串
   */
  export function addChineseUnit(number, decimalDigit = 2) {
    const units = ['万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载'];
    const integer = Math.floor(number);
    const digit = getDigit(integer);
    if (digit <= 4) {
      // 如果数字小于1万，直接返回原数值
      return number.toString();
    }

    let unitGroups = Math.floor((digit - 1) / 4); // 确定单位组（万、亿、兆...）
    if (unitGroups === 0) {
      // 如果在“万”范围内
      return addWan(integer, number, 0, decimalDigit);
    }

    const remainder = digit % 4 || 4;
    const divisor = Math.pow(10, digit - remainder);
    const result = Math.round((number / divisor) * Math.pow(10, decimalDigit)) / Math.pow(10, decimalDigit);

    // 构建单位字符串
    const unitResult = [];
    while (unitGroups > 0) {
      const index = unitGroups % 11; // 每个单位循环
      unitResult.unshift(units[index - 1]); // 获取对应的单位
      unitGroups = Math.floor(unitGroups / 11);
    }

    return `${result}${unitResult.join('')}`;
  }

  /**
   * 根据符号分割字符串
   * @param str 源字符串
   * @param type 返回类型
   * @param args 字符(多参数)
   * @returns 类型数组
   */
  export function strSplitMore(str: string, type: any, ...args) {
    const charArr = args;
    const char = charArr.shift();
    let arr = [];
    let bIndex = 0;
    let eIndex = str.indexOf(char);
    while (eIndex >= 0) {
      const item = str.substring(bIndex, eIndex);
      arr.push(charArr.length > 0 ? strSplitMore(item, type, ...charArr) : item);
      bIndex = eIndex + 1;
      eIndex = str.indexOf(char, bIndex);
    }
    let ret = false;
    for (let i = 0; i < args.length; i++) {
      if (str.indexOf(args[i]) != -1) {
        ret = true;
        break;
      }
    }
    const item = str.substring(bIndex);
    arr.push(ret ? strSplitMore(item, type, ...charArr) : item);
    if (!ret) {
      arr = arr.map(type);
      while (charArr.length > 0) {
        arr = [arr];
        charArr.shift();
      }
    }
    return arr;
  }

  export function getReplaceStr(str: string, ...args): string {
    for (let i = 0; i < args.length; i++) {
      str = str.replace('{' + i + '}', args[i]);
    }
    return str;
  }

  export function getReplaceStrByTag(str: string, tag: string, args: unknown[]): string {
    for (let i = 0; i < args.length; i++) {
      str = str.replace(tag, args[i].toString());
    }
    return str;
  }

  export function getReplaceStrByTags(str: string, tag: string[], args: unknown[]): string {
    for (let i = 0; i < args.length; i++) {
      str = str.replace(tag[i], args[i].toString());
    }
    return str;
  }

  export function splitStrToNum(str: string, separators: string = ',_') {
    if (separators == '') return [Number(str)];
    let ss = separators.substring(0, 1);
    if (str.indexOf(ss) < 0) return [Number(str)];
    const temp = str.split(ss);
    ss = separators.substring(1);
    const result = [];
    const len = temp.length;
    for (let i = 0; i < len; i++) {
      result.push(splitStrToNum(temp[i], ss));
    }
    return result;
  }

  export function splitStr(str: string, separators: string = ',_') {
    if (separators == '') return [str];
    let ss = separators.substring(0, 1);
    if (str.indexOf(ss) < 0) return [str];
    const temp = str.split(ss);
    ss = separators.substring(1);
    const result = [];
    const len = temp.length;
    for (let i = 0; i < len; i++) {
      result.push(splitStrToNum(temp[i], ss));
    }
    return result;
  }

  /** 去除字符串头尾空格或指定字符 */
  export function Trim(tag: string, str = '') {
    TrimStart(tag, str);
    TrimEnd(tag, str);
  }

  /** 去除字符串头部空格或指定字符 */
  export function TrimStart(tag: string, str = '') {
    if (str == null || str == '') {
      return tag.replace(/^\s+/, '');
    } else {
      const rg = new RegExp('^' + str + '+');
      return tag.replace(rg, '');
    }
  }

  //去除字符串尾部空格或指定字符
  export function TrimEnd(tag: string, str = '') {
    if (str == null || str == '') {
      return tag.replace(/\s+$/, '');
    } else {
      const rg = new RegExp(str + '+$');
      return tag.replace(rg, '');
    }
  }

  export function extractString(str: string, match: string = '()'): string {
    const regexp = new RegExp(`(?<=${match[0]})[^${match}]+(?=${match[1]})`);
    const result = str.match(regexp);
    return result ? result[0] : '';
  }

  export function extractStrings(str: string, match: string): string[] {
    const regexp = new RegExp(`(?<=${match[0]})[^${match}]+(?=${match[1]})`, 'g');
    const result = str.match(regexp);
    return result;
  }
}
